/*
package com.xxgc.vfc.gateway.filter.overall;
*/
/*
 *
 *   @Author:max
 *   @Date: 2023/11/1-11-01-15:21
 *   LoggingFilter  +  GlobalFilter
 *//*


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Slf4j
//@Component
public class LoggingFilterGlobalFilter implements GlobalFilter {
    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getPath().value();
        log.info("请求路径："+path);

        //请求头信息
        HttpHeaders headers = request.getHeaders();
        String headersJson = headersToJson(headers);
        log.info("请求头信息（JSON格式）: {}", headersJson);

        //获取请求参数 JSON格式 并用log.info打印
        Flux<DataBuffer> body = request.getBody();
        DataBufferUtils.join(body)
                .map(dataBuffer -> {
                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(bytes);
                    DataBufferUtils.release(dataBuffer);
                    return bytes;
                })
                .map(bytes -> new String(bytes, StandardCharsets.UTF_8))
                .subscribe(bodyStr -> log.info("请求参数（JSON格式）: {}", bodyStr));

        //响应的处理在此处无法获取具体的响应参数，只能获取响应的body内容
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            ServerHttpResponse response = exchange.getResponse();
            //获取响应状态码
            int code = response.getStatusCode().value();
            log.info("状态码："+code);

            //把数据存储到数据库


        }));
    }

    private String headersToJson(HttpHeaders headers) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> headerMap = headers.toSingleValueMap();
        return objectMapper.writeValueAsString(headerMap);
    }
}
*/
